import pygame
from pygame.locals import *
import random

pygame.init()
width,height = 640,480
screen=pygame.display.set_mode((width, height))

black = (0,0,0)
white = (255,255,255)
snake=[]
score = 0
food = False

down = (0,10)
right = (10,0)
up = (0,-10)
left = (-10,0)

for value in range(5):
        snake.append(pygame.Rect(10, 10 + value * 10, 10, 10))
screen = pygame.display.set_mode((width,height))
snake_dir = down
while 1:
    clock = pygame.time.Clock()
    clock.tick(25)
    screen.fill(black)
    
    if food == False:
        egg = pygame.Rect(random.randrange(0,640,10),random.randrange(0,480,10),10,10)
        pygame.draw.rect(screen,white,egg)
        food = True
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
            
    if event.type == pygame.KEYDOWN:
        if event.key == K_w:
            if snake_dir!=down:
                snake_dir = up
        if event.key == K_s:
            if snake_dir!=up:
                snake_dir = down
        if event.key == K_a:
            if snake_dir!=right:
                snake_dir = left
        if event.key == K_d:
            if snake_dir!=left:
                snake_dir = right
                
    go = snake[-1].move(snake_dir)   
    if go in snake:
        pygame.quit()
        print "GameOver"
    snake.append(go)
    
    if go == egg:
        snake.append(go)
        del snake[0]
        food = False
        score = score +10
        print "score:"
        print score
    else:
        del snake[0]
    
    pygame.draw.rect(screen,white,egg)
    for rect in snake:
        pygame.draw.rect(screen,white,rect)
    pygame.display.flip()
